package cn.com.headfree.point.dao.impl;

import cn.com.headfree.entity.TRuleElementInfo;
import cn.com.headfree.point.dao.RuleElementDao;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository("ruleElementDao")
public class RuleElementDaoImpl extends BaseDaoImpl<TRuleElementInfo> implements RuleElementDao {


	@Override
	public List<TRuleElementInfo> findRuleElementListByRuleId(String ruleId) {
		String sql = " select t.* from t_rule_element_info t where t.del_sign = '0' and t.rule_id = ? ";
		List<String> list = new ArrayList<>();
		list.add(ruleId);
		return this.findAllBySQL(TRuleElementInfo.class,sql,null,false,list.toArray());
	}

	@Override
	public void saveRuleElementInfo(TRuleElementInfo ruleElementInfo) {
		this.save(ruleElementInfo);
	}

	@Override
	public void findRuleElementPageListByProjectId(Page<TRuleElementInfo> page ,TRuleElementInfo ruleElementInfo) {
		String sql = " select t.* from t_rule_element_info t where t.del_sign = '0' and t.year = ? and t.project_id = ? and t.org_id=? ";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(sql);
		List<Object> list = new ArrayList<>();
		list.add(ruleElementInfo.getYear());
		list.add(ruleElementInfo.getProjectId());
		list.add(ruleElementInfo.getOrgId());
		if(!StringUtil.isNullOrEmpty(ruleElementInfo.getElementName())){
			stringBuffer.append(" and t.element_name LIKE concat('%',?,'%') ");
			list.add(ruleElementInfo.getElementName());
		}
		stringBuffer.append(" order by t.create_time desc ");
		this.findPageBySql(TRuleElementInfo.class,stringBuffer.toString(),page,list.toArray());
	}
	@Override
	public void updateRuleElementInfo(TRuleElementInfo ruleElementInfo) {
		String sql = " update t_rule_element_info t set ";
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		sb.append(" updater_id=#{updater_id,jdbcType=VARCHAR},");
		sb.append(" updater_name=#{updater_name,jdbcType=VARCHAR},");
		sb.append(" update_time=#{update_time,jdbcType=DATE},");
		sb.append(" element_name=#{element_name,jdbcType=VARCHAR},");
		sb.append(" element_type=#{element_type,jdbcType=VARCHAR},");
		sb.append(" element_level=#{element_level,jdbcType=VARCHAR},");
		sb.append(" element_value=#{element_value,jdbcType=VARCHAR},");
		sb.append(" point=#{point,jdbcType=DOUBLE}");
		sb.append(" where id=#{id,jdbcType=VARCHAR}");
		Map<String,Object> map = new HashMap<>();
		map.put("updater_id",ruleElementInfo.getUpdaterId());
		map.put("updater_name",ruleElementInfo.getUpdaterName());
		map.put("update_time",ruleElementInfo.getUpdateTime());
		map.put("element_name",ruleElementInfo.getElementName());
		map.put("element_type",ruleElementInfo.getElementType());
		map.put("element_level",ruleElementInfo.getElementLevel());
		map.put("element_value",ruleElementInfo.getElementValue());
		map.put("point",ruleElementInfo.getPoint());
		map.put("id",ruleElementInfo.getId());
		this.executeSql(sb.toString(),map);
	}


	@Override
	public void deleteRuleElementInfo(TRuleElementInfo ruleElementInfo) {
		String sql = " update t_rule_element_info t set ";
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		sb.append(" updater_id=#{updater_id,jdbcType=VARCHAR},");
		sb.append(" updater_name=#{updater_name,jdbcType=VARCHAR},");
		sb.append(" update_time=#{update_time,jdbcType=DATE},");
		sb.append(" del_sign=#{del_sign,jdbcType=VARCHAR} ");
		sb.append(" where id=#{id,jdbcType=VARCHAR}");
		Map<String,Object> map = new HashMap<>();
		map.put("updater_id",ruleElementInfo.getUpdaterId());
		map.put("updater_name",ruleElementInfo.getUpdaterName());
		map.put("update_time",ruleElementInfo.getUpdateTime());
		map.put("del_sign",ruleElementInfo.getDelSign());
		map.put("id",ruleElementInfo.getId());
		this.executeSql(sb.toString(),map);
	}

	@Override
	public List<TRuleElementInfo> getRuleElementInfoByRuleIdAndMouldId(String ruleId,String mouldId,String orgId) {
		String sql = " select t.* from t_rule_element_info t where t.del_sign = '0' and t.rule_id = ? and t.project_id = (select id from t_rule_project_info where mould_id = ? and org_id=? ) ";
		List<String> list = new ArrayList<>();
		list.add(ruleId);
		list.add(mouldId);
		list.add(orgId);
		return this.findAllBySQL(TRuleElementInfo.class,sql,null,false,list.toArray());
	}
}
